﻿// ===============================================================================
// XCI.Parent 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using System;
using XCI.Helper;
using XCI.Sys.Helper;
using XCI.Sys.Service;
using XCI.Windows.Forms;
using XCI.Windows.Helper;

namespace XCI.Sys.Forms
{
    public class FrmBaseLogin : FrmBase
    {
        protected virtual System.Windows.Forms.Control EditAccount { get; set; }

        protected virtual System.Windows.Forms.Control EditPassword { get; set; }

        protected virtual System.Windows.Forms.Control BtnLogin { get; set; }

        protected virtual void LoadLoginSetting()
        {
        }

        protected virtual void SaveLoginSetting()
        {
        }

        protected void ExecuteAutoLogin()
        {
            var isSwitch = AppHelper.IsReLogin(SysRuntime.StartupArgs);
            //SplashHelper.CloseSplash();
            //MessageBoxHelper.ShowMessage(isSwitch.ToString());
            if (!isSwitch)
            {
                ThreadHelper.StartThread(() =>
                {
                    try
                    {
                        this.Invoke(new Action(LoginCore));
                    }
                    catch(Exception e)
                    {
                        MessageBoxHelper.ShowError(DebugHelper.BuildAllMessage(e));
                    }
                });
            }
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        protected void LoginCore()
        {
            string account = EditAccount.Text.Trim();
            string password = EditPassword.Text;
            if (string.IsNullOrEmpty(account))
            {
                MessageBoxHelper.ShowWarn("请输入帐号");
                EditAccount.Select();
                return;
            }

            if (string.IsNullOrEmpty(password))
            {
                MessageBoxHelper.ShowWarn("请输入密码");
                EditPassword.Select();
                return;
            }

            //string encryptPassword = EncryptHelper.SymmetricEncrypt(password, EncryptHelper.SecretKey);
            try
            {
                if (BtnLogin != null) BtnLogin.Enabled = false;
                var captcha = string.Empty;
                SplashHelper.ShowProgressPanel(this);
                var reqCap = UserService.Instance.RequireCaptcha(account);
                if (reqCap)
                {
                    //SplashHelper.ShowProgressPanel(this);
                    //SplashHelper.ShowWait("正在生成验证码...", this);
                    var capForm = new FrmCaptcha(account);
                    capForm.ShowDialog();
                    captcha = capForm.Captcha;
                    capForm.Dispose();
                }
                SplashHelper.CloseProgressPanel();

                SplashHelper.ShowProgressPanel(this);
                //SplashHelper.ShowWait("正在登陆...", this);
                var result = SysRuntime.App.UserLogin(account, password, captcha);
                if (result.Success)
                {
                    var user = result.Data;
                    SysRuntime.CurrentUser = user;
                    SaveLoginSetting();
                    SplashHelper.CloseProgressPanel();
                    //SplashHelper.CloseSplash();
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }
                else
                {
                    if (BtnLogin != null) BtnLogin.Enabled = true;
                    SplashHelper.CloseProgressPanel();
                    MessageBoxHelper.ShowWarn(result.Msg);
                }
            }
            finally
            {
                SplashHelper.CloseProgressPanel();
                if (BtnLogin != null) BtnLogin.Enabled = true;
            }
        }
    }
}